package com.x.xiaoshuo.ui.read;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;

import com.x.mvp.base.presenter.ActivityPresenter;
import com.x.service.api.DataRepository;
import com.x.service.entity.Chapter;
import com.x.xiaoshuo.R;
import com.x.xiaoshuo.entity.ReadTheme;
import com.x.xiaoshuo.manager.SettingManager;
import com.x.xiaoshuo.manager.ThemeManager;
import com.x.xiaoshuo.widget.PageBgSelectorView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import timber.log.Timber;


class ReadPresenter extends ActivityPresenter<ReadActivity> {
    DataRepository dataRepository;
    ArrayList<View> bgViews = new ArrayList<>();
    List<ReadTheme> readThemes;

    ReadAdManager readAdManager;


    @Inject
    ReadPresenter(DataRepository dataRepository) {
        this.dataRepository = dataRepository;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        readThemes = ThemeManager.getReaderThemeData(0);
        initBgSeleter();
        readAdManager = new ReadAdManager(view, view.expressAdContainerStart, view.expressAdContainerEnd, view.expressAdContainerEndS);
    }

    public void chapterEnd() {
        readAdManager.render();
    }

    public Bitmap getEndAdBitmap(int remain) {
        return readAdManager.convertEndBitmap(remain);
    }

    public Bitmap getEndAdBitmapSmall() {
        return readAdManager.convertEndSmallBitmap();
    }

    public Bitmap getStartAdBitmap() {
        return readAdManager.convertStartBitmap();
    }

    public void onClickEndAd() {
        readAdManager.click(ReadAdManager.Callback.TYPE_END);
    }

    public void onClickStartAd() {
        readAdManager.click(ReadAdManager.Callback.TYPE_START);
    }


    @Override
    public void onDestroy() {
        readAdManager.destroyAdView();
        super.onDestroy();
    }

    public void getChapterLst(String bookId) {
        addSubscription(dataRepository.getChapters(bookId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io()).subscribe(chapterList -> {
                    view.showBookToc(chapterList.chapterInfo.chapters);
                }, throwable -> {

                }, () -> {

                }));
    }

    public void getChapterRead(String url, final int chapter) {
        addSubscription(dataRepository.getChapterRead(url).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Chapter>() {
                    @Override
                    public void onNext(Chapter data) {
                        view.showChapterRead(data, chapter);
                    }

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                }));
    }

    void initBgSeleter() {
        for (ReadTheme readTheme : readThemes) {
            if (readTheme.type == 0) {
                bgViews.add(createView(readTheme.theme, readTheme.color, readTheme.textColor));
            } else {
                bgViews.add(createBtView(readTheme.theme, readTheme.res, readTheme.textColor));
            }
        }
        view.showBgView(bgViews);
    }

    View createView(int theme, int bgColor, int txtColor) {
        View v = LayoutInflater.from(view).inflate(R.layout.item_page_color, null, false);
        v.setTag(theme);
        PageBgSelectorView p = getPgView(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (View bgView : bgViews) {
                    getPgView(bgView).setSed(false);
                }
                p.setSed(true);
                view.showTheme(theme);
                SettingManager.getInstance().saveReadTheme(theme);
            }
        });
        p.setPbgColor(bgColor);
        p.setTextColor(txtColor);
        if (SettingManager.getInstance().getReadTheme() == theme) {
            p.setSed(true);
        }
        return v;
    }

    View createBtView(int theme, int bitmap, int txtColor) {
        View v = LayoutInflater.from(view).inflate(R.layout.item_page_color, null, false);
        v.setTag(theme);
        PageBgSelectorView p = getPgView(v);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (View bgView : bgViews) {
                    getPgView(bgView).setSed(false);
                }
                p.setSed(true);
                view.showTheme(theme);
            }
        });
        p.setBitMap(bitmap);
        p.setTextColor(txtColor);
        return v;
    }

    PageBgSelectorView getPgView(View v) {
        PageBgSelectorView pageBgSelectorView = (PageBgSelectorView) v.findViewById(R.id.pg);
        return pageBgSelectorView;
    }


    public void openBrowser(Context context, String url) {
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        } catch (Exception e) {
            Timber.e(e, "error while open browser!");
        }
    }


}
